$(function () {
    //使用表单校验插件
    var $form = $("form");
    $form.bootstrapValidator({

        //2. 指定校验时的图标显示，默认是bootstrap风格
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },

        //3. 指定校验字段
        fields: {
            //校验用户名，对应name表单的name属性
            username: {
                validators: {
                    //不能为空
                    notEmpty: {
                        message: '用户名不能为空'
                    },
                    //长度校验
                    stringLength: {
                        min: 4,
                        max: 30,
                        message: '用户名长度必须在6到30之间'
                    },
                    //正则校验
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: '用户名由数字字母下划线和.组成'
                    },

                    callback: {
                        message: "用户名错误"
                    }
                }
            },
            password: {
                validators: {
                    //不能为空
                    notEmpty: {
                        message: '密码不能为空'
                    },
                    //长度校验
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: '用户名长度必须在6到30之间'
                    },
                    //正则校验
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: '用户名由数字字母下划线和.组成'
                    },
                    callback: {
                        message: "密码错误"
                    }
                }
            },
        }

    });

    $form.on('success.form.bv', function (e) {
        e.preventDefault();

        // 登录

        $.ajax({
            url: "/employee/employeeLogin",
            type: "POST",
            data: $form.serialize(),
            success: function (callback) {
                // console.log(callback);   
                if (callback.success) {
                    var href = window.sessionStorage.getItem("href");

                    if (href) {
                        window.location.href = window.sessionStorage.getItem("href");
                        window.sessionStorage.removeItem("href");
                    } else {
                        window.location.href = "index.html";
                    }
                }

                if (callback.error == 1000) {
                    var validator = $form.data('bootstrapValidator'); //获取表单校验实例
                    validator.updateStatus("username", "INVALID", "callback")
                    //username:表单字段的显示的区域是username的,INVALID:转态是失败后显示,callback:调用callback函数
                }

                if (callback.error == 1001) {
                    var validator = $form.data('bootstrapValidator'); //获取表单校验实例
                    validator.updateStatus("password", "INVALID", "callback")
                }
            }
        })

    });

})